<SCRIPT type="text/javascript">
    var {{elem_id}}_nodes = {% raw json_encode(nodes) %};
    var {{elem_id}}_js_tree = new BoTree('{{elem_id}}', '{{taxonomy}}', {{elem_id}}_nodes, "{{reverse_url('taxonomy')}}");
    var {{elem_id}}_form_parsley = null;
    $(document).ready(function() {
        {{elem_id}}_form_parsley = $('#{{elem_id}}_form').parsley();
        $('#{{elem_id}}_edit_dialog_save_btn').click(function(){
            if({{elem_id}}_form_parsley.validate()){
                var params = $('#{{elem_id}}_form').serialize();
                $.post("{{reverse_url('taxonomy')}}", params, function(rsp){
                    if(rsp.success && rsp.message=="200" && rsp.data.length==1){
                        var node = rsp.data[0];
                        var zTree = $.fn.zTree.getZTreeObj('{{elem_id}}');
                        nodes = zTree.getSelectedNodes();
                        treeNode = nodes[0];
                        if (treeNode) {
                            var local_action = $("#{{elem_id}}_save_action").val();
                            if(local_action=='update'){
                                var parentNode = treeNode.getParentNode();
                                zTree.removeNode(treeNode);
                                zTree.addNodes(parentNode, {
                                     id: node[0],
                                     pId: node[1],
                                     name: node[2]
                                });
                            }else{
                                zTree.addNodes(treeNode, {
                                     id: node[0],
                                     pId: node[1],
                                     name: node[2]
                                });
                            }
                        }
                        $('#{{elem_id}}_edit_modal').modal('hide');
                    }else{
                        bo_alert_error(rsp.message);
                    }
                }, "json");
            }
        });
        $.fn.zTree.init($("#{{elem_id}}"), {{elem_id}}_js_tree.settings(), {{elem_id}}_nodes);
    });

</SCRIPT>